﻿using UnityEngine;
using System.Collections;
using System;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using ShooterGameMOD_Besiege.Player;
using ShooterGameMod_Besiege;

namespace ShooterGameMOD_Besiege.Player
{
    public class PlayerScript : MonoBehaviour
    {

        // Use this for initialization

        bool firstFrame = true;

        public static bool playerCreated = false;

        public static GameObject player;

        GameObject PlayerCollider;

        PlayerControllerScript Controller;


        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {
            //判断是否在建造状态下
            if (ShooterGameMod.isBuilding())
            {
                if (player == null)
                {
                    playerCreated = true;
                    CreatePlayer();

                    Debug.Log("scene : " + SceneManager.GetActiveScene().name);
                }
                else
                {
                    if (!player.activeSelf)
                    {

                        player.SetActive(true);

                        playerCreated = true;

                        Debug.Log("ActivePlayer");
                    }

                }

            }
            else
            {
                if (player != null && player.activeSelf)
                {
                    Debug.Log("DeactivePlayer"); ;
                    player.SetActive(false);
                    playerCreated = false;

                }
            }

            if (StatMaster.levelSimulating)
            {
                if (firstFrame)
                {
                    firstFrame = false;

                    Debug.Log("game start");

                    if (playerCreated)
                    {
                        Controller.enabled = true;
                        //GameObject aa = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                        //aa.AddComponent<Rigidbody>();
                        //aa.GetComponent<Rigidbody>().isKinematic = false;
                    }
                }

                if (playerCreated)
                {

                }

            }
            else
            {


                if (!firstFrame)
                {
                    firstFrame = true;

                    if (playerCreated)
                    {
                        Controller.enabled = false;
                        player.transform.position = new Vector3(3, 0, 3);
                        player.transform.eulerAngles = Vector3.zero;

                        Cursor.lockState = CursorLockMode.None;
                        Cursor.visible = true;


                    }




                }


            }
        }

        void CreatePlayer()
        {
            player = null;
            player = GameObject.CreatePrimitive(PrimitiveType.Cube);
            player.name = "Player";
            player.transform.SetParent(gameObject.transform);
            player.transform.position = new Vector3(3, 5, 3);




            Controller = player.AddComponent<PlayerControllerScript>();
            player.AddComponent<PlayerWeaponScript>();
            //player.AddComponent<PlayerCollisionScript>();
            player.AddComponent<CharacterController>();

            //player.AddComponent<Rigidbody>();
            //player.GetComponent<Rigidbody>().freezeRotation = true;
            //player.GetComponent<Rigidbody>().isKinematic = false;
            //player.GetComponent<Rigidbody>().collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;

            PlayerCollider = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            PlayerCollider.transform.SetParent(player.transform);
            PlayerCollider.name = "Player Collider";
            PlayerCollider.transform.position = player.transform.position;
            PlayerCollider.transform.localScale = Vector3.one * 5f;
            PlayerCollider.GetComponent<MeshRenderer>().material.color = new Color(1, 0, 0, 0.1f);
            PlayerCollider.GetComponent<MeshRenderer>().material.shader = Shader.Find("Transparent/Diffuse");
            PlayerCollider.AddComponent<PlayerCollisionScript>();

            Debug.Log("create playering");
        }



        //public override string Name { get; } = "Player";
    }
}
